package com.ayu.gulimall.product.service.impl;

import com.ayu.gulimall.product.vo.Catalog2VO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ayu.gulimall.product.dao.CategoryDao;
import com.ayu.gulimall.product.entity.CategoryEntity;
import com.ayu.gulimall.product.service.CategoryService;
import com.ayu.common.utils.PageUtils;
import com.ayu.common.utils.Query;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> initCateTree() {
        //1得到所有的数据
        List<CategoryEntity> cateList = categoryDao.selectList(null);
        System.out.println("cateList = " + cateList);
        //得到所有的一级菜单
        List<CategoryEntity> collect = cateList.stream()
                .filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(menu->{
                    menu.setChildren(getChildren(menu,cateList));
                    return menu;
                })
                .sorted((c1,c2)->{
                    return (c1.getCatLevel() == null ? 0 : c1.getCatLevel()) - (c1.getCatLevel() == null ? 0 : c1.getCatLevel());
                })
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);
        return collect;
    }

    @Override
    public void removeMenuByIds(List<Long> ids) {
        //TODO 如果当前菜单被其他地方依赖，则不删除

        //删除当前菜单
        categoryDao.deleteBatchIds(ids);
    }

    @Override
    public List<CategoryEntity> getLevel1Categories() {

        List<CategoryEntity> entityList = categoryDao.selectList(new QueryWrapper<CategoryEntity>()
                .eq("parent_cid", 0));

        return entityList;
    }

    @Override
    public Map<String, List<Catalog2VO>> getLevel2Categories() {

        //首先得到所有的一级菜单
        List<CategoryEntity> level1Catalogs = this.getLevel1Categories();
        Map<String, List<Catalog2VO>> map = level1Catalogs.stream().collect(Collectors.toMap(k -> {
            return k.getCatId().toString();
        }, v -> {
            //根据一级菜单id得到所有的二级菜单
            List<CategoryEntity> entityListLevel2 = categoryDao.selectList(new QueryWrapper<CategoryEntity>()
                    .eq("parent_cid", v.getCatId()));
            List<Catalog2VO> catalog2VOList = null;
            if (entityListLevel2 != null) {
                catalog2VOList = entityListLevel2.stream().map(l2 -> {
                    //封装二级菜单
                    return new Catalog2VO(v.getCatId().toString()
                            , null, l2.getCatId().toString()
                            , l2.getName());
                }).collect(Collectors.toList());
                //根据二级菜单找到三级菜单
                catalog2VOList= catalog2VOList.stream().map(l3 -> {
                    List<CategoryEntity> entityListLevel3 = categoryDao.selectList(new QueryWrapper<CategoryEntity>()
                            .eq("parent_cid", l3.getId()));
                    if (entityListLevel3 != null) {
                        //封装三级菜单
                        List<Catalog2VO.Catalog3> catalog3List = entityListLevel3.stream().map(l4 -> {
                            return new Catalog2VO.Catalog3(l3.getId(), l4.getCatId().toString(), l4.getName());
                        }).collect(Collectors.toList());
                        l3.setCatalog3List(catalog3List);
                    }


                    return l3;
                }).collect(Collectors.toList());
            }

            return catalog2VOList;
        }));
        return map;

    }


    public List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        //用递归的方法得到所有的子节点
        List<CategoryEntity> collect = all.stream().filter(categoryEntity -> categoryEntity.getParentCid() == root.getCatId())
                .map(menu -> {
                    menu.setChildren(getChildren(menu, all));
                    return menu;
                })
                .sorted((c1, c2) -> {
                    return (c1.getCatLevel() == null ? 0 : c1.getCatLevel()) - (c1.getCatLevel() == null ? 0 : c1.getCatLevel());
                }).collect(Collectors.toList());
        return collect;
    }

}